package ${package.Controller};

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.common.constant.PermissionConst;
import ${package.Controller}.BaseController;
import com.hyzx.qbasic.common.model.annotation.SysLog;
import ${package.Entity}.${entity};
import ${cfg.reqPackage}.*;
import ${cfg.resPackage}.BaseOperationRes;
import ${cfg.resPackage}.BaseResModel;
import com.hyzx.qbasic.common.util.BaseResUtils;
import ${package.Service}.${table.serviceName};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;

/**
 * ${table.comment} 控制器类
 *
 * @author ${author}
 * @since ${date}
 */
<#assign entityVarName ="${cfg.entityName?uncap_first}">
<#assign serviceVarName ="${entityVarName}Service">
<#assign templatePath ="${cfg.routePath?substring(1)}">
<#--获取主键字段-->
<#list table.fields as field>
    <#if field.keyFlag>
        <#assign primaryKey = field>
    </#if>
</#list>

@Api(tags = "${table.comment}", description = "${table.controllerName}")
@Controller
@RequestMapping("${cfg.routePath}")
public class ${table.controllerName} extends BaseController {

    @Autowired
    private ${table.serviceName} ${serviceVarName};

    @ApiIgnore
    @SysLog("${table.comment}首页")
    @GetMapping("/list")
    public String list(Model model) {
        //model.addAttribute("addFunc", getFunction(PermissionConst.));
        //model.addAttribute("editFunc", getFunction(PermissionConst.));
        //model.addAttribute("deleteFunc", getFunction(PermissionConst.));
        return "${templatePath}/list";
    }

    @ApiOperation("获取${table.comment}列表")
    @GetMapping("/get${cfg.entityName}List")
    @ResponseBody
    public BaseResModel<PageInfo<${entity}>> get${cfg.entityName}List(@Valid ${cfg.getListReqName} req, BindingResult result) {
        return BaseResUtils.success(${serviceVarName}.getPaged${cfg.entityName}List(req));
    }

    @ApiIgnore
    @SysLog("新增${table.comment}页")
    @GetMapping("/add")
    public String add() {
        return "${templatePath}/add";
    }

    @ApiOperation("新增${table.comment}表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody ${cfg.addReqName} req, BindingResult result) {
        BaseOperationRes res = ${serviceVarName}.add${cfg.entityName}(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除${table.comment}记录")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody ${cfg.deleteReqName} req, BindingResult result) {
        BaseOperationRes res = ${serviceVarName}.delete${cfg.entityName}(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑${table.comment}页")
    @GetMapping("/edit")
    public String edit() {
        return "${templatePath}/edit";
    }

    @ApiOperation("编辑${table.comment}表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody ${cfg.editReqName} req, BindingResult result) {
        BaseOperationRes res = ${serviceVarName}.edit${cfg.entityName}(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取${table.comment}信息")
    @GetMapping("/get${cfg.entityName}Info")
    @ResponseBody
    public BaseResModel<${entity}> get${cfg.entityName}Info(@Valid ${cfg.getInfoReqName} req, BindingResult result) {
        return BaseResUtils.success(${serviceVarName}.get${cfg.entityName}InfoById(req));
    }
}